package com.sunwayhorizo.health.hl7.rim;

import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.DISABILITYCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.EDUCATIONLEVELCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.ETHNICGROUPCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.IDENTITYCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.LIVINGARRNGMNTCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.MARITALSTATUSCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.ORIGINCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.PROFESSIONCODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.RACECODE;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.RELIGIOUSAFLTNCODE;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;


import com.sunwayhorizo.health.hl7.common.DataTypeUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.ListUtil;
import com.sunwayhorizo.health.hl7.datatypes.AD;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.datatypes.common.ADXPImpl;
import com.sunwayhorizo.health.hl7.factories.ReferenceModifier;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyAddrParts;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyAddrUses;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyAddrs;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyCdSets;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("PSN")
public class Person extends LivingSubject implements Serializable
{

    /**
	 * 
	 */
    protected static final long serialVersionUID = 1L;


    public Person() throws Hl7Exception
    {
    }


    public Person(ReferenceModifier referencemodifier)
    {
        super(referencemodifier);
    }


    // 保存一个person地址
    protected void newAddr(AD addr)
    {
        RimCoreEntyAddrs entyAddrs = new RimCoreEntyAddrs();
        entyAddrs.setFormatted(addr.formatted().toString());
        entyAddrs.setEntyAddrUses(addr.use().toSet());
        entyAddrs.setEntyAddrParts(addr.toList());
        this.addEntyAddrs(entyAddrs);
    }


    /**
     * 设置Person的地址，保存和修改地址都用此方法
     * 
     * @param addr
     *            person地址信息
     */
    @SuppressWarnings("rawtypes")
    public void setAddr(AD addr)
    {
        List mList = addr.toList();
        List<RimCoreEntyAddrs> addrList = this.getEntyAddrsList();
        if (ListUtil.isNotEmpty(addrList))
        {
            for (int i = 0; i < addrList.size(); i++)
            {
                RimCoreEntyAddrs addrs = addrList.get(i);
                String addrFormat = "";
                List<RimCoreEntyAddrParts> addrParts = addrs.getEntyeAddrPartsList();
                if (ListUtil.isNotEmpty(addrParts))
                {
                    for (int j = 0; j < addrParts.size(); j++)
                    {
                        RimCoreEntyAddrParts addPart = addrParts.get(j);
                        for (int k = j; k < mList.size(); k++)
                        {
                            ADXPImpl ad = (ADXPImpl) mList.get(k);
                            if (ad.literal().toString().equals("<Null:NI>") 
                                    || addPart.getPartType().equals("<Null:NI>") 
                                    || (addPart.getPartType().equals(""))
                                    || addPart.getPartType().equals(ad.partType().code().toString()))
                            {
                                addrFormat += ad.literal().toString();
                                addPart.setLiteral(ad.literal().toString());
                                addPart.setPartType(ad.partType().code().toString());
                                addPart.setPartOrderNum((long) k);
                                break;
                            }
                        }
                    }
                }
                addrs.setFormatted(addrFormat);
            }
        }
        else
        {
            newAddr(addr);
        }
    }


    /**
     * 得到person的地址信息
     * 
     * @return
     * @throws Hl7Exception
     */
    @SuppressWarnings("rawtypes")
    public AD getAddr() throws Hl7Exception
    {

        List<RimCoreEntyAddrs> addsList = this.getEntyAddrsList();
        AD ad = null;
        if (addsList != null && !addsList.isEmpty())
        {
            RimCoreEntyAddrs entyAddrs = (RimCoreEntyAddrs) addsList.get(0);
            List mList = entyAddrs.getEntyeAddrPartsList();
            int size = mList.size();
            String value[] = new String[size];
            String type[] = new String[size];
            for (int i = 0; i < size; i++)
            {
                RimCoreEntyAddrParts partAddr = (RimCoreEntyAddrParts) mList.get(i);
                value[i] = partAddr.getLiteral();
                type[i] = partAddr.getPartType();
            }
            RimCoreEntyAddrUses addrUses = (RimCoreEntyAddrUses) entyAddrs.getEntyAddrUsesList().get(0);
            String useCode = addrUses.getUse();
            ad = DataTypeUtil.getAD(value, type, useCode);
        }

        return ad;
    }


    /**
     * 设置person的残疾程度信息
     * 
     * @param ce
     *            残疾程度信息
     */
    public void setDisabilityCode(CE ce)
    {

        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, DISABILITYCODE);
        }
        else
        {
            this.setPrDisabilityCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person的残疾程度信息
     * 
     * @return CE
     * @throws Hl7Exception
     */
    public CE getDisabilityCode() throws Hl7Exception
    {
        if (this.getPrDisabilityCodeEtsId().equals(DISABILITYCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrDisabilityCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return (DataTypeUtil.getCE(entityCdSets));
                }
            }
        }
        else
            return (DataTypeUtil.getCE(this.getPrDisabilityCodeEtsId()));
        return null;
    }


    /**
     * 设置person的教育程度信息
     * 
     * @param ce
     *            CE类型的教育程度信息
     */
    public void setEducationLevelCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, EDUCATIONLEVELCODE);
        }
        else
        {
            this.setPrEducationLevelCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person的教育程度信息
     * 
     * @return CE
     * @throws Hl7Exception
     */
    public CE getEducationLevelCode() throws Hl7Exception
    {
        if (this.getPrEducationLevelCodeEtsId().equals(EDUCATIONLEVELCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrEducationLevelCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entityCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPrEducationLevelCodeEtsId());
        return null;
    }


    /**
     * 设置person的名族团体
     * 
     * @param ce
     *            名族团体编码
     */
    public void setEthnicGroupCode(CE ce)
    {

        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, ETHNICGROUPCODE);
        }
        else
        {
            this.setPrEthnicGroupCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person的名族团体
     * 
     * @return CE
     * @throws Hl7Exception
     */
    public CE getEthnicGroupCode() throws Hl7Exception
    {
        if (null != this.getPrEthnicGroupCodeEtsId() && this.getPrEthnicGroupCodeEtsId().equals(ETHNICGROUPCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrEthnicGroupCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return (DataTypeUtil.getCE(entityCdSets));
                }
            }
        }
        else
            return (DataTypeUtil.getCE(this.getPrEthnicGroupCodeEtsId()));
        return null;
    }


    /**
     * 设置person的居住情况(如独立家庭;机构;养老院;延续护理设施;退休社区等)
     * 
     * @param ce
     *            CE类型的居住情况
     */
    public void setLivingArrangementCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, LIVINGARRNGMNTCODE);
        }
        else
        {
            this.setPrLivingArrngmntCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person的居住情况
     * 
     * @return CE
     * @throws Hl7Exception
     */
    public CE getLivingArrangementCode() throws Hl7Exception
    {
        if (this.getPrLivingArrngmntCodeEtsId().equals(LIVINGARRNGMNTCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrLivingArrngmntCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entityCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPrLivingArrngmntCodeEtsId());
        return null;
    }


    /**
     * 设置perosn婚史
     * 
     * @param ce
     *            婚史信息
     */
    public void setMaritalStatusCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, MARITALSTATUSCODE);
        }
        else
        {
            this.setPrMaritalStatusCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person婚史
     * 
     * @return CE
     * @throws Hl7Exception
     */
    public CE getMaritalStatusCode() throws Hl7Exception
    {
        if (null != this.getPrMaritalStatusCodeEtsId() && this.getPrMaritalStatusCodeEtsId().equals(MARITALSTATUSCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrMaritalStatusCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entityCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPrMaritalStatusCodeEtsId());
        return null;
    }


    /**
     * 设置person的种族信息
     * 
     * @param ce
     */
    public void setRaceCode(CE ce)
    {

        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, RACECODE);
        }
        else
        {
            this.setPrRaceCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person的种族信息
     * 
     * @return
     * @throws Hl7Exception
     */
    public CE getRaceCode() throws Hl7Exception
    {
        if (this.getPrRaceCodeEtsId().equals(RACECODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrRaceCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return (DataTypeUtil.getCE(entityCdSets));
                }
            }
        }
        else
            return (DataTypeUtil.getCE(this.getPrRaceCodeEtsId()));
        return null;
    }


    /**
     * 设置person职业信息
     * 
     * @param ce
     *            职业信息
     */
    public void setProfessionCode(CE ce)
    {
        setRimCoreEntyCdSets(ce, PROFESSIONCODE);
    }


    /**
     * 得到person职业信息
     * 
     * @return CE
     */
    public CE getProfessionCode()
    {
        for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
        {
            if (PROFESSIONCODE.toString().equals(entityCdSets.getAttributeTypeCode()))
            {
                return DataTypeUtil.getCE(entityCdSets);
            }
        }
        return null;
    }


    /**
     * 设置person的宗教信仰
     * 
     * @param ce
     *            宗教信仰信息
     */
    public void setReligiousAffiliationCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, RELIGIOUSAFLTNCODE);
        }
        else
        {
            this.setPrReligiousAfltnCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person的宗教信仰信息
     * 
     * @return CE
     * @throws Hl7Exception
     */
    public CE getReligiousAffiliationCode() throws Hl7Exception
    {
        if (this.getPrReligiousAfltnCodeEtsId().equals(RELIGIOUSAFLTNCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrReligiousAfltnCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entityCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPrReligiousAfltnCodeEtsId());
        return null;
    }


    /**
     * 设置person的身份（如军人的身份）
     * 
     * @param ce
     */
    public void setIdentityCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, IDENTITYCODE);
        }
        else
        {
            this.setPrIdentityCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到person的身份
     * 
     * @return CE
     * @throws Hl7Exception
     */
    public CE getIdentityCode() throws Hl7Exception
    {
        if (null != this.getPrIdentityCodeEtsId() && this.getPrIdentityCodeEtsId().equals(IDENTITYCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrIdentityCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entityCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPrIdentityCodeEtsId());
        return null;
    }


    /**
     * 设置person的籍贯
     * 
     * @param ce
     */
    public void setOriginCodeEtsID(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, ORIGINCODE);
        }
        else
        {
            this.setPrOriginCodeEtsId((ce.code().toString().equals("<Null:NullFlavor: NI>") ? "" : ce.code().toString()));
        }
    }


    /**
     * 得到person的籍贯
     * 
     * @return
     * @throws Hl7Exception
     */
    public CE getOriginCodeEtsID() throws Hl7Exception
    {
        if (null !=this.getPrOriginCodeEtsId() &&  this.getPrOriginCodeEtsId().equals(ORIGINCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getPrOriginCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entityCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPrOriginCodeEtsId());
        return null;
    }

    // Person
    @Column(name = "PR_EDUCATION_LEVEL_CODE_ETS_ID")
    protected String prEducationLevelCodeEtsId = EDUCATIONLEVELCODE.toString();
    @Column(name = "PR_LIVING_ARRNGMNT_CODE_ETS_ID")
    protected String prLivingArrngmntCodeEtsId = LIVINGARRNGMNTCODE.toString();
    @Column(name = "PR_MARITAL_STATUS_CODE_ETS_ID")
    protected String prMaritalStatusCodeEtsId = MARITALSTATUSCODE.toString();
    @Column(name = "PR_RELIGIOUS_AFLTN_CODE_ETS_ID")
    protected String prReligiousAfltnCodeEtsId = RELIGIOUSAFLTNCODE.toString();

    @Column(name = "PR_DISABILITY_CODE_ETS_ID")
    protected String prDisabilityCodeEtsId = DISABILITYCODE.toString();
    @Column(name = "PR_ETHNIC_GROUP_CODE_ETS_ID")
    protected String prEthnicGroupCodeEtsId = ETHNICGROUPCODE.toString();
    @Column(name = "PR_RACE_CODE_ETS_ID")
    protected String prRaceCodeEtsId = RACECODE.toString();
    // 对应军人的身份属性代码
    @Column(name = "PR_IDENTITY_CODE_ETS_ID")
    protected String prIdentityCodeEtsId = IDENTITYCODE.toString();
    // 对应人的属性籍贯代码
    @Column(name = "PR_ORIGIN_CODE_ETS_ID")
    protected String prOriginCodeEtsId = ORIGINCODE.toString();


    protected String getPrDisabilityCodeEtsId()
    {
        return prDisabilityCodeEtsId;
    }


    protected void setPrDisabilityCodeEtsId(String prDisabilityCodeEtsId)
    {
        this.prDisabilityCodeEtsId = prDisabilityCodeEtsId;
    }


    protected String getPrEthnicGroupCodeEtsId()
    {
        return prEthnicGroupCodeEtsId;
    }


    protected String getPrRaceCodeEtsId()
    {
        return prRaceCodeEtsId;
    }


    protected void setPrRaceCodeEtsId(String prRaceCodeEtsId)
    {
        this.prRaceCodeEtsId = prRaceCodeEtsId;
    }


    protected void setPrEthnicGroupCodeEtsId(String prEthnicGroupCodeEtsId)
    {
        this.prEthnicGroupCodeEtsId = prEthnicGroupCodeEtsId;
    }


    protected String getPrIdentityCodeEtsId()
    {
        return prIdentityCodeEtsId;
    }


    protected void setPrIdentityCodeEtsId(String prIdentityCodeEtsId)
    {
        this.prIdentityCodeEtsId = prIdentityCodeEtsId;
    }


    public String getPrOriginCodeEtsId()
    {
        return prOriginCodeEtsId;
    }


    public void setPrOriginCodeEtsId(String prOriginCodeEtsId)
    {
        this.prOriginCodeEtsId = prOriginCodeEtsId;
    }


    protected void setPrEducationLevelCodeEtsId(String prEducationLevelCodeEtsId)
    {
        this.prEducationLevelCodeEtsId = prEducationLevelCodeEtsId;
    }


    protected String getPrEducationLevelCodeEtsId()
    {
        return prEducationLevelCodeEtsId;
    }


    protected void setPrLivingArrngmntCodeEtsId(String prLivingArrngmntCodeEtsId)
    {
        this.prLivingArrngmntCodeEtsId = prLivingArrngmntCodeEtsId;
    }


    protected String getPrLivingArrngmntCodeEtsId()
    {
        return prLivingArrngmntCodeEtsId;
    }


    protected void setPrMaritalStatusCodeEtsId(String prMaritalStatusCodeEtsId)
    {
        this.prMaritalStatusCodeEtsId = prMaritalStatusCodeEtsId;
    }


    protected String getPrMaritalStatusCodeEtsId()
    {
        return prMaritalStatusCodeEtsId;
    }


    protected void setPrReligiousAfltnCodeEtsId(String prReligiousAfltnCodeEtsId)
    {
        this.prReligiousAfltnCodeEtsId = prReligiousAfltnCodeEtsId;
    }


    protected String getPrReligiousAfltnCodeEtsId()
    {
        return prReligiousAfltnCodeEtsId;
    }

}
